package com.lincoln.system.app.api;

import com.lincoln.bean.ApiCode;
import com.lincoln.bean.ApiCodeException;
import com.lincoln.bean.BaseSecurityApi;
import com.lincoln.entity.role.DbRole;
import com.lincoln.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleApi extends BaseSecurityApi {
    @Autowired
    RoleService roleService;

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('role_admin')")
    public ApiCode list(Integer pageNo, Integer pageSize, String name) {
        if (pageNo == null) {
            pageNo = 0;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        PageRequest pageRequest = roleService.buildPageRequest(pageNo, pageSize, Sort.Order.desc("createTime"));
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotEmpty(name)) {
            params.put("name", name);
        }
        Page<DbRole> page = roleService.findPage(pageRequest, params);
        return ApiCode.build(page);
    }

    @GetMapping("/{roleIdOrName}")
    @PreAuthorize("hasAuthority('role_admin')")
    public ApiCode info(@PathVariable String roleIdOrName) throws Exception {
        if (StringUtils.isEmpty(roleIdOrName)) {
            throw new ApiCodeException(10001);
        }
        DbRole role = null;
        try {
            Long id = Long.parseLong(roleIdOrName);
            role = roleService.findById(id);
        } catch (NumberFormatException e) {
            role = roleService.findByUniqueForOne("name", roleIdOrName);
        }
        if (role == null) {
            throw new ApiCodeException(ApiCode.build(ApiCode.FAIL, "等级不存在!"));
        }
        return ApiCode.build(role);
    }

    @PutMapping("/add")
    @PreAuthorize("hasAuthority('role_admin')")
    public ApiCode add(String name, String permissions) throws Exception {
        if (StringUtils.isEmpty(name)) {
            return ApiCode.build(ApiCode.FAIL, "name can not be null!");
        }
        if (StringUtils.isEmpty(permissions)) {
            return ApiCode.build(ApiCode.FAIL, "permissions can not be null!");
        }
        roleService.save(name, permissions);
        return ApiCode.findCode(ApiCode.SUCCESS);
    }

    @PostMapping("/{id}")
    @PreAuthorize("hasAuthority('role_admin')")
    public ApiCode edit(@PathVariable Long id, String name, String permissions) throws Exception {
        if (id == null) {
            throw new ApiCodeException(10001);
        }
        if (StringUtils.isEmpty(name) && StringUtils.isEmpty(permissions)) {
            throw new ApiCodeException(10001);
        }
        roleService.update(id, name, permissions);
        return ApiCode.findCode(ApiCode.SUCCESS);
    }
}
